package com.example.callfor.api;

import com.example.callfor.model.User;
import com.example.callfor.model.UserMapper;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
public class UserApi {

    @Resource
    private UserMapper userMapper;

    /**
     * 用户登入
     * @param name
     * @param password
     * @return
     */
    @PostMapping("/login")
    @ResponseBody
    public Object login(String name, String password, HttpServletRequest request){
        User user = userMapper.selectByName(name);
        if (user == null || !user.getPassword().equals(password)){
            System.out.println("登入失败");
            return new User();
        }
        //登入成功，存入session
        HttpSession session = request.getSession(true);
        session.setAttribute("user",user);
        return user;
    }

    /**
     * 用户注册
     * @param groupName
     * @param companyName
     * @param password
     * @param price
     * @return
     */
    @PostMapping("/register")
    public Object register(String groupName,String companyName,String name,String tel,String password,String price){
        try {
            User user = new User();
            user.setGroupName(groupName);
            user.setCompanyName(companyName);
            user.setName(name);
            user.setTel(tel);
            user.setPassword(password);
            user.setPrice(price);
            userMapper.addUser(user);
            user.setId(1);
            return user;
        }catch (DuplicateKeyException e){
            return new User();
        }
    }

    /**
     * 登入用户信息
     * @param request
     * @return
     */
    @GetMapping("/userInfo")
    public Object getUserInfo(HttpServletRequest request){
        try {
            HttpSession session = request.getSession(false);
            User user = (User) session.getAttribute("user");
            user = userMapper.selectByName(user.getCompanyName());
            return user;
        }catch (NullPointerException e){
            return new User();
        }
    }

    /**
     * 获取生产厂家信息
     * @return
     */
    @GetMapping("/comInfo")
    public Object getComInfo(){
        return userMapper.selectComInfo();
    }

    /**
     * 获取招标单位信息
     * @return
     */
    @GetMapping("/unitInfo")
    public Object getUnitInfo(){
        return userMapper.selectUnitInfo();
    }
    /**
     * 修改公司信息
     * @param user
     * @return
     */
    @PostMapping("/update")
    public Object update(User user){
        return userMapper.update(user);
    }

    /**
     * 删除用户信息
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public Object delete(int id){
        return userMapper.deleteUSer(id);
    }
    /**
     * 退出登入
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public Object logOut(HttpServletRequest request){
        request.getSession().removeAttribute("user");
        return 1;
    }

}
